using System.Collections.Generic;
using System.Linq;
using Android.App;
using Android.Views;
using Android.Widget;
using CapstoneProject.Models;

namespace CapstoneProject
{
    class AccountListAdapter : BaseAdapter
    {
        private IEnumerable<Account> accounts;
        private Activity context;

        public AccountListAdapter(Activity context, IEnumerable<Account> accounts)
        {
            this.context = context;
            this.accounts = accounts;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var view = (convertView ?? context.LayoutInflater.Inflate(Resource.Layout.AccountListItem, parent, false)) as LinearLayout;
            var account = accounts.ElementAt(position);

            string accountBalance = string.Format(account.AccountName + ": {0:C}", account.Balance);
            view.FindViewById<TextView>(Resource.Id.AccountName).Text = accountBalance;
            return view;
        }

        public override int Count
        {
            get { return accounts.Count(); }
        }

        public override Java.Lang.Object GetItem(int position)
        {
            return null;
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public Account GetAccount(int position)
        {
            return accounts.ElementAt(position);
        }
    }
}